let Ag3DTilesLayer = agcim.layer.Ag3DTilesLayer;
let StyleCondition = agcim.renderer.StyleCondition;

var viewerList = [];
class Commonload {
    /**
        * 加载geoserver的服务WMS
        * @author chzm(合肥自定义)
        * @param {object} viewer viewer 
        * @param {object} url url 
        */
    addWMSLayerByUrl(viewer, url) {
        // url = "http://192.168.1.176:8080/agserver/FSQdata2D/wms?service=WMS&version=1.1.0&request=GetMap&layers=FSQdata2D%3A%E6%88%BF%E5%B1%B1%E5%8C%BA%E5%B8%82%E6%94%BF%E9%81%93%E6%A1%A5%E8%AE%BE%E6%96%BD&bbox=115.96315552600004%2C39.68400282300007%2C116.21784387500009%2C39.80663891000006&width=768&height=369&srs=EPSG%3A4326&format=application/openlayers"
        var realUrl = url.split("?");
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        var layersname = encodeURI(theRequest.layers);
        var wms = new Cesium.WebMapServiceImageryProvider({
            url: realUrl,
            layers: layersname,
            parameters: {
                transparent: true,//是否透明
                service: 'WMS',
                format: 'image/png',
                srs: 'EPSG:4326',
                styles: ''
            }
        });
        viewer.imageryLayers.addImageryProvider(wms);
    }

    /**
        * 添加一个wfs的图层，获取json文件，执行回调函数
        * @author chzm (合肥自定义)
        * @param {*} viewer 
        * @param {*} url 服务地址
        * @param {*} color 颜色样式默认黄色
        */
    addWFSLayerByUrl(viewer, url, color) {
        if (color) {
            color = Cesium.Color.YELLOW;
        }
        $.ajax({
            url: url,
            cache: false,
            async: true,
            success: function (data) {
                if (data && data.features) {
                    var optioncolor = {
                        stroke: color,
                        strokeWidth: 8,
                        markerSize: 20,
                    };
                    var datasource = Cesium.GeoJsonDataSource.load(data, optioncolor);
                    viewer.dataSources.add(datasource);
                }

            },
            error: function (data) {
                console.log("error=" + data);
            }
        });
    }

    /**
        * 添加一个3dtileset的图层
        * @author chzm (合肥自定义)
        * @param {*} url 服务地址
        */
    addAg3dtileset(url) {
        var ag3DTilesLayer = new Ag3DTilesLayer(CIM.viewer);
        var tileset = ag3DTilesLayer.add(url);
    }

    /**
        * 修改3d图层透明度
        * @author chzm (合肥自定义)
        * @param {*} tileset 3dtilsets
        * @param val 透明度值（0~1）
    */
    setAg3dtilesetOpacity(tileset, val) {
        var styleCondition = new StyleCondition(tileset);
        styleCondition.setOpacity(val);
    }

    /**
        * 设置图层显隐
        * @author chzm (合肥自定义)
        * @param {*} id id
        * @param flag boolean
    */
    setShowOrHide(id, flag) {
        for (var i = 0; i < CIM.layerTree.operationLayers.ag3dtilesLayers.length; i++) {
            if (CIM.layerTree.operationLayers.ag3dtilesLayers[i].agMetaData.id == id) {
                CIM.layerTree.operationLayers.ag3dtilesLayers[i].show = flag;
                return true;
            }
        }
        for (var j = 0; j < CIM.layerTree.operationLayers.imageryLayers.length; j++) {
            if (CIM.layerTree.operationLayers.imageryLayers[j].agMetaData.id == id) {
                CIM.layerTree.operationLayers.imageryLayers[j].show = flag;
                return true;
            }
        }
    }

     /**
        * 默认视角
        * @author chzm (合肥自定义)
    */
    flyToViewer() {
        var url =
            "/agsupport-rest/io/imagestore/getAll?mapType=2&type=spot&regionId=" + CIM.commonParam.regionId;
        var promise =  agcim.net.axiosWraper.getData(url);
        promise.then(function (data) {
            if (data && data.success) {
                if(data.content && data.content != '暂无数据!'){
                    for (var i = 0; i < data.content.length; i++) {
                        var information = JSON.parse(data.content[i].information);
                        data.content[i].information = information;
                    }
                    viewerList = data.content;
                }
            }
        }).then(function () {
            var heading = 6.283185307179583;
            var pitch = -0.5050675111373115;
            var roll = 6.2831853071795845;
            if (viewerList && viewerList.length > 0) {
                for (var i = 0; i < viewerList.length; i++) {
                    if (viewerList[i].name.indexOf("默认视点") > -1) {
                        var position = viewerList[i].information.position;
                    } if (viewerList[i].name.indexOf("全局视点") > -1) {
                        var fullposition = viewerList[i].information.position;
                    }
                }
            }

            if (position) {
                var point = new Cesium.Cartesian3(
                    position.x,
                    position.y,
                    position.z, 

                );
            }
            if (!position && fullposition) {
                var point = new Cesium.Cartesian3(
                    fullposition.x,
                    fullposition.y,
                    fullposition.z,
                );
            }
            if (!position && !fullposition) {
                return;
            }
            CIM.viewer.camera.flyTo({
                destination: point,
                orientation: {
                    heading: heading,
                    pitch: pitch,
                    roll: roll,
                },
            });
        })
    }
   
}
export default Commonload;